//
//  XDContactsListViewController.m
//  iphone.mobileoa
//
//  Created by 李亚春 on 13-5-29.
//  Copyright (c) 2013年 Epoint. All rights reserved.
//

#import "XDContactsListViewController.h"
#import "DBFrameService.h"
#import "ContactsListCell.h"
#import "QueryContactsTask.h"
#import "XDContactsDetailViewController.h"
#import "TSActionSheet.h"

@interface XDContactsListViewController ()
{
    NSMutableArray *dataArray;
    NSMutableArray *backFlagList;
    UIBarButtonItem *rightButton;
    NSString *selectedOuGuid;
}
@end

@implementation XDContactsListViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    backFlagList = [[NSMutableArray alloc]init];
    
    [self getData:@""];
    
    //    [self.navigationItem.backBarButtonItem setAction:@selector(backLogic)];
    //
    //    UIBarButtonItem* back = [[UIBarButtonItem alloc]initWithTitle:@"返回" style:UIBarButtonItemStyleBordered target:self action:@selector(backLogic)];
    //    self.navigationItem.leftBarButtonItem = back;
    //    [back release];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)dealloc {
    [_searchBar release];
    [_tv release];
    [super dealloc];
}
- (void)viewDidUnload {
    [self setSearchBar:nil];
    [self setTv:nil];
    [super viewDidUnload];
}

#pragma UI刷新
-(void)refreshUIWithThreadGuid:(NSString *)threadGuid andData:(NSObject *)obj
{
    [self hideMBPHUD];
    dataArray = (NSMutableArray *)[obj retain];
    [self.tv reloadData];
}

#pragma 获取本地数据库数据

-(void)getData:(NSString *)_parentguid
{
    dataArray = [[DBFrameService getContactsListByParentGuid:_parentguid]retain];
}

-(void)backLogic
{
    if ([backFlagList count]==0) {
        [self.navigationController popViewControllerAnimated:YES];
    }
    else if([backFlagList count]==1)
    {
        [self getData:@""];
        [self.tv reloadData];
        self.title = @"通讯录";
    }
    else
    {
        NSString *guid = [backFlagList objectAtIndex:[backFlagList count]-2];
        [self getData:guid];
        [self.tv reloadData];
        self.title = [DBFrameService getOuNameByOuGuid:guid];
    }
    
    if ([backFlagList count]>0) {
        NSInteger count = [backFlagList count];
        [backFlagList removeObjectAtIndex:count - 1];
    }
}

#pragma searchbar

- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar{
    [searchBar resignFirstResponder];
    [self doSearch:searchBar];
}

- (void)doSearch:(UISearchBar *)searchBar{
    [self showMBPHUD];
    NSMutableDictionary *params = self.getPassDictionary;
    [params setObject:searchBar.text forKey:@"keyword"];
    [[[QueryContactsTask alloc]initWithContext:self andParams:params]startTask];
}

#pragma tableview

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *WebinfolistItem = @"CellItem";
    ContactsListCell *cell = (ContactsListCell *)[tableView dequeueReusableCellWithIdentifier:WebinfolistItem];
    if (cell==nil) {
        NSArray *nib = [[NSBundle mainBundle]loadNibNamed:@"ContactsListCell" owner:self options:nil];
        cell = [nib objectAtIndex:0];
    }
    NSMutableDictionary *m = [dataArray objectAtIndex:indexPath.row];
    cell.lbName.text = [m objectForKey:@"name"];
    if ([[m objectForKey:@"type"]isEqualToString:@"user"]) {
        cell.lbTitle.text = [m objectForKey:@"title"];
        cell.lbOuName.text = [m objectForKey:@"ouname"];
        cell.ivHead.image = [UIImage imageNamed:@"img_user"];
    }
    else
    {
        cell.lbOuName.text = @"部门";
        cell.lbOuName.hidden = YES;
        [cell.lbName setFrame:CGRectMake(cell.lbName.frame.origin.x, cell.lbName.frame.origin.y, cell.lbName.frame.size.width, 45)];
        cell.lbTitle.hidden = YES;
        cell.ivHead.image = [UIImage imageNamed:@"img_ou"];
    }
    return cell;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return dataArray.count;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 59;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSMutableDictionary *m = [dataArray objectAtIndex:indexPath.row];
    NSString *guid = [m objectForKey:@"guid"];
    NSString *type = [m objectForKey:@"type"];
    if ([type isEqualToString:@"ou"]) {
        selectedOuGuid = [guid retain];
//        NSLog(@"选中部门为：%@:%@",selectedOuGuid,[m objectForKey:@"name"]);
        
        rightButton = [[UIBarButtonItem alloc]initWithTitle:@"上级" style:UIBarButtonItemStyleBordered target:self action:@selector(showActionSheet:forEvent:)];
        self.navigationItem.rightBarButtonItem = rightButton;
        
        [self getData:guid];
        [self.tv reloadData];
        [backFlagList addObject:guid];
        self.title = [m objectForKey:@"name"];
    }
    else
    {
        NSMutableDictionary *params = [[NSMutableDictionary alloc]init];
        [params setObject:guid forKey:@"userguid"];
        [params setObject:@"详细信息" forKey:@"viewtitle"];
        XDContactsDetailViewController *vc = [[XDContactsDetailViewController alloc]initWithParams:params];
        [self.navigationController pushViewController:vc animated:YES];
    }
}

-(void)showActionSheet:(id)sender forEvent:(UIEvent*)event
{
    NSMutableArray *ous =  [DBFrameService getParentOusByOuguid:selectedOuGuid];
//    NSLog(@"choose dp:%@",selectedOuGuid);
    
    NSMutableArray *fullous = [[NSMutableArray alloc]init];
    NSMutableDictionary *rootou = [[NSMutableDictionary alloc]init];
    [rootou setObject:@"根部门" forKey:@"ouname"];
    [rootou setObject:@"" forKey:@"ouguid"];
    [fullous addObject:rootou];
    
    for (int i=ous.count-1; i>=0; i--) {
        [fullous addObject:[ous objectAtIndex:i]];
    }
    
    TSActionSheet *actionSheet = [[TSActionSheet alloc] initWithTitle:@"上级部门"];
    for (int i=0; i<fullous.count; i++) {
        NSMutableDictionary *ou = [fullous objectAtIndex:i];
        NSString *ouguid = [ou objectForKey:@"ouguid"];
        NSString *ouname = [ou objectForKey:@"ouname"];
        
        
        
//        NSLog(@"%@ %@",ouname,ouguid);
        [actionSheet addButtonWithTitle:ouname block:^{
            [self getData:ouguid];
            selectedOuGuid = ouguid;
            [self.tv reloadData];
            self.title = ouname;
            if ([ouguid isEqualToString:@""]) {
                self.navigationItem.rightBarButtonItem = nil;
            }
        }];
    }
    actionSheet.cornerRadius = 2;
    [actionSheet showWithTouch:event];
    //    TSActionSheet *actionSheet = [[TSActionSheet alloc] initWithTitle:@"上级部门"];
    //
    //    NSMutableArray *guids = [[NSMutableArray alloc]init];
    //    for (int i=0; i<backFlagList.count; i++) {
    //        [guids addObject:[backFlagList objectAtIndex:i]];
    //    }
    //    [actionSheet addButtonWithTitle:@"根部门" block:^{
    //        [self getData:@""];
    //        [self.tv reloadData];
    //        self.title = @"通讯录";
    //        self.navigationItem.rightBarButtonItem = nil;
    //    }];
    //    NSMutableArray *ous = [DBFrameService getOunameArrayByGuids:guids];
    //    for (int i=0; i<ous.count-1; i++) {
    //        NSMutableDictionary *ou = [ous objectAtIndex:i];
    //        [actionSheet addButtonWithTitle:[ou objectForKey:@"name"] block:^{
    //            [self getData:[ou objectForKey:@"guid"]];
    //            [self.tv reloadData];
    //            self.title = [ou objectForKey:@"name"];
    //        }];
    //    }
    //
    //    actionSheet.cornerRadius = 2;
    //    [actionSheet showWithTouch:event];
}

@end
